package com.sd.web.im;

import com.sd.domain.ImCollect;
import com.sd.service.ImCollectService;
import com.sd.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * im-收藏模块
 * @author yexinming
 * @date 2020-07-29
 */
@RestController
@RequestMapping("/im/collect")
@Api(value = "/im/collect", produces = "im-收藏模块")
public class CollectController {
    /**
     * 日志记录组件
     */
    private static final Logger logger = LoggerFactory.getLogger(CollectController.class);

    @Autowired
    private ImCollectService collectService;

    /**
     * 显示图片
     */
    @GetMapping("/picture/{id}")
    public void viewLogo(@PathVariable("id") Long id,
                         HttpServletRequest request, HttpServletResponse response) {
        FileInputStream fis = null;
        try {
            ImCollect collect = collectService.getById(id);

            String filePath = collect.getFilePath();
            if(!StringUtils.isEmpty(filePath)){
                String[] strings = filePath.split("\\.");
                String imageSuffix = "jpg";
                if(strings.length>0){
                    imageSuffix=strings[1];
                }
                File file = new File(collect.getFilePath());
                fis = new FileInputStream(file);
                byte[] bytes = new byte[fis.available()];
                fis.read(bytes);
                response.setContentType("image/"+imageSuffix);
                OutputStream out = response.getOutputStream();
                out.write(bytes);
                out.flush();
            }
        } catch (Exception e) {
            logger.error("error", e);
        } finally {
            if(fis != null ) {
                try {
                    fis.close();
                } catch (IOException e) {
                    logger.error("error", e);
                }
            }
        }
    }
    /**
     * 根据坐席id查询列表
     * @param agentId
     * @return
     */
    @ApiOperation(value = "查询坐席收藏列表", notes="查询坐席收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agentId", value = "坐席id", paramType = "query", required = true, dataType = "string")
    })
    @GetMapping("/{agentId}")
    public R listByAgentId(@PathVariable("agentId") String agentId){
      try {
        return R.ok().put("data",collectService.listByAgentId(agentId));
      }catch (Exception e){
        logger.error("error",e);
        return R.error();
      }
    }
    /**
     * 保存
     * @param collect 收藏对象
     */
    @ApiOperation(value = "保存收藏列表", notes="保存收藏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collect", value = "收藏对象", required = true, dataType = "object"),
            @ApiImplicitParam(name = "file", value = "文件", required = false, dataType = "object")
    })
    @PostMapping("/")
    public R save(ImCollect collect,MultipartFile file) {
        try {
         collectService.save(collect,file);
         return R.ok();
        }catch (Exception e){
          return R.error();
        }
    }
}
